home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Unix / Programming / DrawingServant / Source / tester.c < prev   
Encoding:
C/C++ Source or Header  |  1993-07-08  |  2.7 KB  |  108 lines

  1. /* a simple program to illustrate use of DrawingServant */
  2. /* ask the user to click somewhere inside the drawing window, then draw
  3. 1000 lines with the click as one endpoint */
  4. /* this is NOT a good example of anything except how a vanilla looking
  5. c program can invoke DrawingServant as a server to the NeXT window server */
  6.  
  7. #import <math.h>
  8. #import <signal.h>
  9. #import <stdlib.h>
  10.  
  11. #define DEFHEIGHT 500
  12. #define DEFWIDTH 500
  13. #define DRAWSERVER "DrawingServant"
  14. static int outpipe[2],inpipe[2];
  15. static int pid;
  16. char outstring[200];
  17. int height=DEFHEIGHT;
  18. int width=DEFWIDTH;
  19.  
  20. #define SENDPS write(outpipe[1],outstring,strlen(outstring)+1)
  21. #define GETACK do{\
  22.         read(inpipe[0],outstring,sizeof(outstring)-1);}\
  23.         while(strncmp(outstring,"Ok",2))
  24.  
  25. main()
  26. {
  27.     char hstr[20],wstr[20],instr[20],outstr[20];
  28.     int i;
  29.     float lx,ly;
  30.     void next_line();
  31.     void killDS();
  32.     void DSdied();
  33.  
  34.     float allones = (float)pow((double)2.,(double)31.);
  35. #define FRAND ((float)rand())/allones
  36.  
  37.     /* fork a process and assign some pipes.  return if
  38.     unsuccessful */
  39.     if( pipe(outpipe)== -1)
  40.             return -1;
  41.     if( pipe(inpipe)== -1)
  42.             return -1;
  43.     if( (pid=fork())== -1)
  44.             return -1;
  45.  
  46.     if(pid==0) {
  47.         sprintf(hstr,"h %d ",height);
  48.         sprintf(wstr,"w %d ",width);
  49.         sprintf(outstr,"i %d",outpipe[0]);
  50.         sprintf(instr,"o %d",inpipe[1]);
  51.         execl(DRAWSERVER,DRAWSERVER,hstr,wstr,instr,outstr,0);
  52.         exit(0);
  53.     }
  54.  
  55.     /* try to coordinate the activities of the caller and the
  56.     callee.  If the servant died, this will signal main to exit
  57.     gracefully.  atexit kills the servant when the main goes
  58.     nighty-night; signal kills the child or itself on ^C of
  59.     "quit" selected from the menu  */
  60.     atexit(killDS);
  61.     signal(SIGINT,killDS);
  62.     signal(SIGCHLD,DSdied);
  63.   
  64.     /* send the servant window all the way to the front - even above
  65.     the terminal window */
  66.     sprintf(outstring,"DSfront\n");
  67.     SENDPS;
  68.     GETACK;
  69.     /* get a string for the mouse; on a mouse click, we will get a
  70.     string which is either "left mouse at ..." or "right mouse at ..."*/
  71.     sprintf(outstring,"DSmouseon\n");
  72.     SENDPS;
  73.     GETACK;
  74.  
  75.     printf("Click inside the drawing window to become main,\n");
  76.     printf("Then again for one end point.\n");
  77.  
  78.     /* do nothing until the string comes */
  79.     do {
  80.         read(inpipe[0],outstring,sizeof(outstring)-1);
  81.     } while (sscanf(outstring,"%*s mouse %f %f",&lx,&ly)!=2 );
  82.  
  83.     for(i=0;i<1000;i++) {
  84.         next_line(lx,ly,500*FRAND,500*FRAND);
  85.     }
  86.     printf("Click inside the terminal window\nThen Return to continue...");
  87.     getchar();
  88. }
  89.  
  90. void next_line(i,j,k,l)
  91. float i,j,k,l;
  92. {
  93.     sprintf(outstring,"newpath %f %f moveto %f %f lineto stroke\n",
  94.                                 i,j,k,l);
  95.     SENDPS;
  96.     GETACK;
  97. }        
  98. void DSdied()
  99. {
  100.     /* call _exit directly and avoid atexit call */
  101.     _exit();
  102. }
  103. void killDS()
  104. {
  105.     sprintf(outstring,"DSquit\n");
  106.     SENDPS;
  107. }
  108.